package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜品控制器
 */
@RequestMapping("/admin/dish")
@RestController
@Api("菜品控制器")
@Slf4j
public class DishController {
    @Resource
    private DishService dishService;
    // 写请求方法:三要素

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);//后绪步骤实现
        return Result.success(dishVO);
    }

    @DeleteMapping
    public Result<String> shanChuByIds(@RequestParam List<Long> ids){
        log.info("[删除的菜品列表] ids:{}",ids);
        Integer tag = dishService.batchDelete(ids);
        return Result.success(tag > 0?"删除成功":"删除失败");
    }


    // 分页查询 PageResult(结果集和条数)
    @GetMapping("/page")
    public Result<PageResult> findByPage(DishPageQueryDTO dishPageQueryDTO){
        log.info("[分页查询] dto:{}",dishPageQueryDTO);
        PageResult pageResult = dishService.findByPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    @PostMapping
    public Result<String> addDish(@RequestBody DishDTO dishDTO){
        log.info("[新增菜品] dishDto:{}",dishDTO);
        Integer tag  = dishService.addDish(dishDTO);
        return Result.success(tag > 0 ?"新增成功":"新增失败");
    }
}
